package com.guigu.www.tangyingmeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.mapper.BespeakPojectMapper;
import com.guigu.www.mapper.BespeakPojectTypeMapper;
import com.guigu.www.mapper.QuotaInnerMapper;
import com.guigu.www.mapper.QuotaMapper;
import com.guigu.www.pojo.*;
import com.guigu.www.tangyingmeng.service.TymBespeakPojectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TymBespeakPojectServiceImpl implements TymBespeakPojectService {

    @Autowired
    private BespeakPojectMapper pojectMapper;

    @Autowired
    private BespeakPojectTypeMapper typeMapper;

    @Autowired
    private QuotaMapper quotaMapper;

    @Autowired
    private QuotaInnerMapper quotaInnerMapper;


    //分页查询所有项目
    @Override
    public PageVo<BespeakPoject> selpojectall(Map<String, String> map) {
        QueryWrapper<BespeakPoject> wrapper = new QueryWrapper<>();
        wrapper.like("bes_pro_id", map.get("findid")).like("bes_pro_name", map.get("findname")).like("bes_pro_type_id", map.get("findtpid"));
        IPage<BespeakPoject> page = new Page<>(Integer.valueOf(map.get("index")), Integer.valueOf(map.get("pageSize")));
        page = pojectMapper.selectPage(page, wrapper);
        PageVo<BespeakPoject> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());
        for (BespeakPoject poject:pageVo.getRows()) {
            poject.setBespeakPojectType(typeMapper.selectById(poject.getBesProTypeId()));
        }
        return pageVo;
    }

    //查询所有项目类型
    @Override
    public List<BespeakPojectType> selpojecttypeall() {
        List<BespeakPojectType> list = typeMapper.selectList(null);
        return list;
    }

    //根据项目编号查询项目的具体信息
    @Override
    public BespeakPoject selpojectone(String besproid) {
        BespeakPoject poject = pojectMapper.selectById(besproid);
        QueryWrapper<QuotaInner> wrapper = new QueryWrapper<>();
        wrapper.eq("bes_pro_id",besproid);
        QuotaInner inner = quotaInnerMapper.selectOne(wrapper);
        Quota quota = quotaMapper.selectById(inner.getQuotaId());
        poject.setQuota(quota);
        return poject;
    }

    //修改项目
    @Override
    public Boolean pojectupdate(BespeakPoject bespeakPoject) {
        int update = pojectMapper.updateById(bespeakPoject);
        if (update>0) {
            QueryWrapper<QuotaInner> wrapper = new QueryWrapper<>();
            wrapper.eq("bes_pro_id",bespeakPoject.getBesProId());
            QuotaInner inner = quotaInnerMapper.selectOne(wrapper);
            Quota quota = new Quota(inner.getQuotaId(),bespeakPoject.getBesProName(),bespeakPoject.getQuota().getQuotaConsult(),bespeakPoject.getQuota().getQuotaUnit(),null);
                int qin = quotaMapper.updateById(quota);
                if(qin>0){
                    return true;
                }
                return false;
        }else {
            return false;
        }
    }

    @Override
    public Boolean pojectadd(BespeakPoject bespeakPoject) {
        // System.out.println(bespeakPoject);
        int insert = pojectMapper.insert(bespeakPoject);
        if (insert>0) {
            Quota quota = new Quota(null,bespeakPoject.getBesProName(),bespeakPoject.getQuota().getQuotaConsult(),bespeakPoject.getQuota().getQuotaUnit(),null);
            int insertq = quotaMapper.insert(quota);
            QuotaInner quotaInner = new QuotaInner(null,quota.getQuotaId(),bespeakPoject.getBesProId());
            int qin = quotaInnerMapper.insert(quotaInner);
            if(qin>0){
                return true;
            }
            return false;
        }else {
            return false;
        }
    }

    @Override
    public Boolean delpoject(String id) {
        int delete = pojectMapper.deleteById(id);
        if (delete>0) {
            return true;
        }else {
            return false;
        }
    }

}
